/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package View.ClusterizationPanel;

import java.awt.Color;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author vf
 */

public class CustomOptionsActionsTableModel extends AbstractTableModel{


    private CustomOptionsActionsWindow main;
    private int[] assignedClusters;

    public CustomOptionsActionsTableModel(CustomOptionsActionsWindow main){
        this.main=main;
        assignedClusters = new int[main.getNumberOfActions()];
        for (int i=0;i<assignedClusters.length;i++)
            assignedClusters[i]=-1;
        System.out.println("number of actions to assign: "+assignedClusters.length);
    }

    @Override
    public String getColumnName(int col) {
        if(col==0) return "No";
        else if (col==1) return "Cluster";
        else if (col==2) return "Color";
        else return main.getAttributeName(col-3);
    }

    public int getRowCount() {
        return main.getNumberOfActions();
    }

    public int getColumnCount() {
        return main.getAttributeNbr()+3;
    }

    public Object getValueAt(int rowIndex, int columnIndex) {
        if(columnIndex==0)return rowIndex+1;
        else if(columnIndex==1){
            if (assignedClusters[rowIndex]>=0)
                return main.getClusterName(assignedClusters[rowIndex]);
            else
                return new String("none");
        }
        else if(columnIndex==2){
            if (assignedClusters[rowIndex]>=0)
                return main.getClusterColor(assignedClusters[rowIndex]);
            else
                return Color.WHITE;
        }
        else return main.getValue(rowIndex,columnIndex-3);
    }

    public Class getColumnClass(int columnIndex) {
        if (columnIndex == 2)
            return Color.class;
        else
            return java.lang.Object.class;
    }

    public boolean isCellEditable(int row, int col) {
        return false;
    }

    public void assignCluster(int actionIndex, int clusterIndex){
        assignedClusters[actionIndex] = clusterIndex;
    }

    public void clearAssignedCluster(int actionIndex){
        assignedClusters[actionIndex]=-1;
    }

    public void clearAllActionsAssignedTo(int clusterIndex){
        for (int i=0;i<assignedClusters.length;i++){
            if (assignedClusters[i]==clusterIndex)
                assignedClusters[i]=-1;
        }
    }

    public int getActionsCountAssignedTo(int clusterIndex){
        int count=0;
        for (int i=0;i<assignedClusters.length;i++){
            if (assignedClusters[i]==clusterIndex)
                count++;
        }
        return count;
    }

    public int getActionAssignedTo(int clusterIndex,int actionIndex){
        int count = -1, realActionIndex = -1;
        for (int i=0;i<assignedClusters.length;i++){
             if (assignedClusters[i]==clusterIndex)
                count++;
             if (count == actionIndex && realActionIndex == -1)
                 realActionIndex=i;
        }
        if (realActionIndex == -1){
            System.out.println("realActionIndex=-1 in CustomActionsTableModel, getActionOfCluster("+clusterIndex+", "+actionIndex);
            realActionIndex=0;
        }
        return realActionIndex;
    }

    //public void setValueAt(Object value, int row, int col) {
    //    if(col==1) main.setClusterName(row,value.toString());
    //}

}
